package com.green.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.green.entity.MyUser;
import com.green.entity.StockEntity;
import com.green.listener.SimpleReadListener;
import com.green.listener.UserReadListener;
import com.green.service.ExcelReadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class ExcelReadServiceImpl implements ExcelReadService {


    /**
     * 读取简单的Excel文件
     */
    @Override
    public void readSimpleExcel(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, StockEntity.class, new PageReadListener<StockEntity>(dataList -> {
                for (StockEntity stockEntity : dataList) {
                    log.info("读取到一条数据:{}", stockEntity);
                }
            })).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 使用自定义读取监听器
     *
     * @param file
     */
    @Override
    public void readSimpleExcel2(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            //使用自定义读取监听器
            EasyExcel.read(inputStream, StockEntity.class, new SimpleReadListener()).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取多个sheet的Excel文件
     *
     * @param file
     */
    @Override
    public void readMultiSheetExcel(MultipartFile file) {
//        try {
//            //读取所有的
//            EasyExcel.read(file.getInputStream(), StockEntity.class, new SimpleReadListener()).doReadAll();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        //读取指定sheet
        try (ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build()) {
            ReadSheet sheet1 = EasyExcel.readSheet(0).head(StockEntity.class).registerReadListener(new SimpleReadListener()).build();
            ReadSheet sheet2 = EasyExcel.readSheet(1).head(StockEntity.class).registerReadListener(new SimpleReadListener()).build();
            excelReader.read(sheet2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 多行头读取
     * headRowNumber指定从哪一行开始读
     *
     * @param file
     */
    @Override
    public void readMultiHeadExcel(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            //使用自定义读取监听器
            EasyExcel.read(inputStream, StockEntity.class, new SimpleReadListener())
                    .sheet(2)
                    .headRowNumber(2)
                    .doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void readByConverterData(MultipartFile file) {
        UserReadListener userReadListener = new UserReadListener();
        try {
            EasyExcel.read(file.getInputStream(), MyUser.class, userReadListener)
                    .sheet(3)
                    .doRead();
            List<MyUser> data = userReadListener.getData();
            data.forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
